package com.lottery.im.module.main.ui;

import android.content.Intent;
import android.os.Message;
import com.lottery.im.R;
import com.lottery.im.base.BaseActivity;
import com.lottery.im.common.WeakReferenceHandler;
import com.lottery.im.constant.SpKeyConstant;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.StatusBarHelper;

public class SplashActivity extends BaseActivity {

  private static int DELAY = 1000;
  private MyHandler handler;

  private class MyHandler extends WeakReferenceHandler<BaseActivity> {

    public MyHandler(BaseActivity reference) {
      super(reference);
    }

    protected void handleMessage(BaseActivity reference, Message msg) {
      startActivity(new Intent(SplashActivity.this, MainActivity.class));
      finish();
    }
  }

  @Override protected int getLayoutResId() {
    return R.layout.activity_splash;
  }

  @Override protected void initPresenter() {
    init();
  }

  private void init() {
    StatusBarHelper.translucent(this);
    StatusBarHelper.setStatusBarLightMode(this);
    handler = new MyHandler(this);
    boolean isFirst = (boolean) SPUtils.get(this, SpKeyConstant.KEY_FIRST_ENTRY, true);
    if (isFirst) {
      SPUtils.put(this, SpKeyConstant.KEY_FIRST_ENTRY, false);
      handler.postDelayed(() -> {
        Message m = handler.obtainMessage(1);
        handler.sendMessage(m);
      }, DELAY);
    } else {
      handler.postDelayed(() -> {
        boolean isLogin = (boolean) SPUtils.get(mActivity, SpKeyConstant.IS_LOGIN, false);
        if (isLogin) {
          Message m = handler.obtainMessage(2);
          handler.sendMessage(m);
        } else {
          Message m = handler.obtainMessage(0);
          handler.sendMessage(m);
        }
      }, DELAY);
    }
  }
}
